package com.freez.spark.sql.operation;

import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.SparkSession;
import org.apache.spark.sql.types.DataTypes;
import org.apache.spark.sql.types.DecimalType;
import org.apache.spark.sql.types.StructField;
import org.apache.spark.sql.types.StructType;

import java.util.Properties;

/**
 * FREEDOM  2021 人生苦短，不妨一试
 * FileName: ReadSql.java
 * Author: zcs
 * Date: 2021年-12月-07日 周二 18:35
 * Description: SparkSQL的基本读取sql
 */
public class ReadSql {
    public static void main(String[] args) {
        SparkConf conf = new SparkConf();
        conf.setAppName("sparkSQL").setMaster("local");
        JavaSparkContext jsc = new JavaSparkContext(conf);

        SparkSession sparkSession = SparkSession
                .builder()
                .appName("SparkSQLTest").master("local")
                .config("spark.some.config.option", "some-value")
                .getOrCreate();
        run(sparkSession);
//        Dataset<Row> df = sparkSession.read().jdbc(sqlURL,"student",properties);
//        df.select(df.col("*"));
//        df.printSchema();
//        df.filter("").show();
        sparkSession.stop();

    }

    public static void run(SparkSession sparkSession) {
        String sqlURL = "jdbc:mysql://192.168.1.131:3306/spark_SQL_test";
        Properties properties = new Properties();
        properties.put("user", "root");
        properties.put("password", "aosatech");
        properties.put("driver", "com.mysql.cj.jdbc.Driver");
        Dataset<Row> dataset = sparkSession.read().jdbc(sqlURL, "student", properties);
        StructType schema = dataset.schema();
        StructField[] fields = schema.fields();
//        for (StructField a : fields){
//            System.out.println(a.dataType().typeName());
//        }
//        dataset.select("*");
//        dataset.col("id").$plus(100);
        DecimalType decimalType = DataTypes.createDecimalType();
//        decimalType.isWiderThan()
        double a = 2.22;
        Object obj = a;


//        decimalType.isWiderThan();
//        System.out.println(a.getClass().toString());

        System.out.println(obj.getClass().toString());
//        dataset.show(false)
    }
}